<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="12000px; ">
    <el-form ref="formRef" :model="formData" :rules="formRules" label-width="150px" v-loading="formLoading">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入商家名称" />
      </el-form-item>
     
      <el-form-item label="logo地址" prop="logo">
        <UploadImg v-model="formData.logo" :limit="1" :is-show-tip="false" />
      </el-form-item>
      <el-form-item label="身份类型" prop="identityType">
        <el-radio-group v-model="formData.identityType">
          <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.MERCHANT_IDENTITY_TYPE)" :key="dict.value" :label="dict.value">
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入商家联系电话" />
      </el-form-item>
      <el-form-item label="商家描述" prop="descrption">
        <el-input type="textarea" v-model="formData.description" placeholder="请输入商家描述" />
      </el-form-item>
      <el-form-item label="经营品类" prop="categoryId">
        <ProductCategorySelect v-model="formData.categoryId" />
      </el-form-item>
     
      <el-form-item label="发货方式" prop="deliveryType">
        <el-radio-group v-model="formData.deliveryType">
          <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.MERCHANT_DELIVERY_TYPE)" :key="dict.value" :label="dict.value">
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.AUDIT_STATUS)" :key="dict.value" :label="dict.value">
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalName">
        <el-input v-model="formData.legalName" placeholder="请输入法人姓名" />
      </el-form-item>
      <el-form-item label="法人手机号" prop="legalPhone">
        <el-input v-model="formData.legalPhone" type="number" placeholder="请输入法人手机号" />
      </el-form-item>
      <el-form-item label="法人身份证号" prop="legalIdCardNo">
        <el-input v-model="formData.legalIdCardNo" type="number" :max="18" placeholder="请输入法人身份证号码" />
      </el-form-item>
      <el-form-item label="身份证生效日期" prop="legalIdCardEffectDate">
        <el-date-picker
v-model="formData.legalIdCardEffectDate" type="date" value-format="x"
          placeholder="选择法人身份证生效日期" />
      </el-form-item>
      <el-form-item label="身份证失效日期" prop="legalIdCardExpireDate">
        <el-date-picker
v-model="formData.legalIdCardExpireDate" type="date" value-format="x"
          placeholder="选择法人身份证失效日期" />
      </el-form-item>
      <el-form-item label="经营范围" prop="businessScope">
        <el-input type="textarea" v-model="formData.businessScope" placeholder="请输入经营范围" />
      </el-form-item>
      <el-form-item label="统一社会信用代码" v-if="formData.identityType!=1" prop="creditCode">
        <el-input v-model="formData.creditCode" placeholder="请输入统一社会信用代码" />
      </el-form-item>
      <el-form-item label="信用代码生效日期" v-if="formData.identityType!=1" prop="creditCodeEffectDate">
        <el-date-picker v-model="formData.creditCodeEffectDate" type="date" value-format="x" placeholder="统一社会信用证生效日期" />
      </el-form-item>
      <el-form-item label="信用代码失效日期" v-if="formData.identityType!=1"  prop="creditCodeExpireDate">
        <el-date-picker v-model="formData.creditCodeExpireDate" type="date" value-format="x" placeholder="统一社会信用证失效日期" />
      </el-form-item>
      <el-form-item label="所在地" prop="areaId">
        <el-tree-select
          v-model="formData.areaId"
          :data="areaList"
          :props="defaultProps"
          :render-after-expand="true"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入商家联系地址" />
      </el-form-item>
     
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { MerchantApi, MerchantVO } from '@/api/mall/product/merchant';
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict';

import ProductCategorySelect from '../category/components/ProductCategorySelect.vue';
import * as AreaApi from '@/api/system/area'
import { defaultProps } from '@/utils/tree'
/** 商家信息 表单 */
defineOptions({ name: 'InfoForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  logo: undefined,
  name: undefined,
  identityType: undefined,
  description: undefined,
  categoryId: undefined,
  type: undefined,
  deliveryType: undefined,
  status: undefined,
  legalName: undefined,
  legalPhone: undefined,
  legalIdCardNo: undefined,
  legalIdCardEffectDate: undefined,
  legalIdCardExpireDate: undefined,
  businessScope: undefined,
  creditCode: undefined,
  creditCodeEffectDate: undefined,
  creditCodeExpireDate: undefined,
  areaId: undefined,
  address: undefined,
  mobile: undefined,
})
const formRules = reactive({
  name: [{ required: true, message: '商家名称不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

const areaList = ref([]) // 地区列表

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  areaList.value = await AreaApi.getAreaTree()
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MerchantApi.getInfo(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MerchantVO
    if (formType.value === 'create') {
      await MerchantApi.createInfo(data)
      message.success(t('common.createSuccess'))
    } else {
      await MerchantApi.updateInfo(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    identityType:undefined,
    id: undefined,
    logo: undefined,
    name: undefined,
    description: undefined,
    categoryId: undefined,
    type: undefined,
    areaId:undefined,
    deliveryType: undefined,
    status: undefined,
    legalName: undefined,
    legalPhone: undefined,
    legalIdCardNo: undefined,
    legalIdCardEffectDate: undefined,
    legalIdCardExpireDate: undefined,
    businessScope: undefined,
    creditCode: undefined,
    creditCodeEffectDate: undefined,
    creditCodeExpireDate: undefined,
    address: undefined,
    mobile: undefined,
  }
  formRef.value?.resetFields()
}
</script>